﻿using System.Web.Mvc;
using Inovout.Web.Mvc;
using Inovout.YunYao.Models;
using Inovout.YunYao.Services;
using System.Linq;

namespace Inovout.YunYao.Console.Web.Controllers
{
    [AllowAnonymous]
    public class LocationController : RepositoryController<Location>
    {
        protected virtual ILocationService LocationService
        {
            get { return (ILocationService)base.RepositoryService; }
        }

        public override ActionResult Index()
        {
            return RedirectToAction("Top");
        }

        public ActionResult Top()
        {
            //位置
            ViewBag.BreadCrumb = new Navigation { Text = "位置", Url = base.Url.Action("Index") };
            return View("List", LocationService.FindAllTop());
        }

        public ActionResult Children(Location location)
        {
            //江苏省->南京市
            Navigation breadCrumb = new Navigation { Text = "位置", Url = base.Url.Action("Index") };
            Location parentLocation = location;
            while (parentLocation != null)
            {
                breadCrumb.Items.Insert(0,
                                new Navigation
                                {
                                    Text = parentLocation.Name
                                    ,
                                    Url = Url.RouteUrl(new { controller = "Location", action = "Children", id = parentLocation.Id })
                                });
                parentLocation = parentLocation.Parent;
            }
            ViewBag.BreadCrumb = breadCrumb;
            return View("List", location.Children);
        }

    }
}
